1. Deleting Tables
Deleting tables is extremely easy. A simple call to CloudTableClient.DeleteTable will obliterate
all your tables. Note that deleting large tables can take some time. If
you try to re-create a table quickly after it is deleted, you could get
an error message.
Example 1 shows the code for deleting
the ContactsTable. Once you delete a
table, it is immediately inaccessible for operations. Unlike other
distributed storage services, there is no propagation delay that lets
other operations succeed.
Example 1. Deleting the table
account.CreateCloudTableClient().DeleteTable("ContactsTable");
|
The REST equivalent is just as simple. Just send an HTTP DELETE to the table’s URL and you’re
done.
DELETE /Tables('ContactTable') HTTP/1.1
User-Agent: Microsoft ADO.NET Data Services
x-ms-date: Mon, 20 Apr 2009 18:34:37 GMT
Authorization: SharedKeyLite sriramk:vhJAA1od931iUvj8MEBu4pb1hu3fIILiA15Ndf3n+8Y=
2. Deleting EntitiesDeleting entities is similar to deleting tables. You retrieve the
entity you want to delete through a query, call DataServiceContext.DeleteObject, and save
changes back. Example 2 shows the code to
do this.
Example 2. Deleting entities
CloudStorageAccount.Parse(ConfigurationSettings.AppSettings ["DataConnectionString"]); var svc = new TestDataServiceContext(account.TableEndpoint.ToString(), account.Credentials); var item = (from contact in svc.CreateQuery<Contact>("ContactTable") where contact.Name == "Steve Jobs" select contact).Single(); svc.DeleteObject(item); svc.SaveChanges();
|
However, this means you can delete entities only when you’ve
already queried and retrieved them from table storage. This is
inefficient when deleting large sets of entities, and it seems
counterintuitive to query entities just to delete them. You can work
around this by creating a dummy local entity with the right partition
key and row key, attaching it to the DataServiceContext, and then deleting
it.
Example 3 shows how to
do this. This mandates that you already know the partition key and row
key for the item you want to delete. In Example 3, you would replace
CorrectPartitionKey and CorrectRowKey with the actual partition key
and row key, respectively.
Example 3. Deleting entities without querying
var svc = new TestDataServiceContext(account.TableEndpoint.ToString(), account.Credentials); var item = new Contact("CorrectPartitionKey", "CorrectRowKey"); svc.AttachTo("ContactTable", item, "*"); svc.DeleteObject(item); svc.SaveChanges();
|
How does this work? Note the wildcard character passed as a
parameter to the AttachTo method.
Here, you’re bringing an empty object into the DataServiceContext’s view of the local world,
deleting it, and then using the wildcard to say that
“regardless of what changes have happened on the server,
replace with changes with this version.” Since the only
change here is to delete the object, the deletion is propagated to the
server.
If you inspect the HTTP traffic, you’ll notice one key difference
between normal deletes and this variant. Normally, the delete operation
specifies an ETag to say, “I’m trying
to perform an operation on this version of the object.” In this variant,
you see the header If-Match: *, which
essentially says, “Perform this operation on any version of the object.”
There are trade-offs when picking either of these variants. Pick
the former when you want to delete a specific version of your entity and
you don’t care about any intermediate updates. Use the latter version
when you want to obliterate the entity, no matter what has happened
since you last retrieved it.